<?php  
/**
* 
*/
class PermissionController extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();

		$this->load->model('Permission_Model');
	}

	public function index(){
		$session_data = $this->session->userdata('logged_in');

		$data['resultPermission'] = $this->Permission_Model->getAllPermission();

		$this->load->view("include/header");
		$this->load->view("Permission/index",$data);
		$this->load->view("include/footer");

	}

	public function create(){
		$session_data = $this->session->userdata('logged_in');

		$this->load->library('form_validation');
		$this->form_validation->set_rules('name', 'ชื่อ Permission', 'trim|required');
		$this->form_validation->set_rules('description', 'รายละเอียด', 'trim|required');
		$this->form_validation->set_message('required','กรุณาใส่ %s ');

		if ($this->form_validation->run() == FALSE)
		{
			$this->index();
		}
		else
		{

			$newid = "P" . sprintf( '%02s' , ((int)substr( $this->Permission_Model->getLastPermissionID() , 1) + 1) );

			$newPer = array(
				'per_id' => $newid,
				'per_name' => $this->input->post('name'),
				'per_description' => $this->input->post('description'),
				'per_createdate' => date("Y-m-d H:i:s"),
				'per_createuser' => $session_data['userID'],
				'per_status' => 'A'
				);

			$this->Permission_Model->insert($newPer);
			$this->index();
		}
	}

	public function edit($id){

		$data['dataLoad']= $this->Permission_Model->getPermissionWithID($id);

		$this->load->view("include/header");
		$this->load->view("Permission/edit",$data);
		$this->load->view("include/footer");
	}
}
?>